
from pyramid.view import view_config
from pyramid.view import view_defaults
from pyramid.response import Response
from pyramid.httpexceptions import HTTPFound
import json
from ..utils.Hash.HashUtil import HashUtil
from ..utils.Image.ImageUtil import ImageUtil
from ..utils.DB import MySqlCRUD
import time, os, uuid, shutil, hashlib
import base64
from ..utils.Queue import UploadTasks
from ..utils.SMS import MobSMS
from ..utils.Cache.TokenCache import TokenCache
from ..utils.Cache.ChecksumCache import ChecksumCache
import logging

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
log = logging.getLogger(__name__)

from WebBase import Base

class ImageFetch(Base):

    @view_config(route_name='AppWebImageFetchRoute', renderer='json')
    def AppWebImageFetchRoute(self):
        postParams =  self.request.POST
        category = postParams.getone('category')
        usetType = postParams.getone('type')
        tel = postParams.getone('tel')

        selectSQL = '''
            SELECT material_info.MD5, material_info.name, material_info.category, material_info.material, material_info.size, material_info.weight, material_info.aperture, material_info.price, material_info.description, material_info.path, material_info.url
                FROM material_info
                INNER JOIN
                (
                    user_image_info INNER JOIN user_info
                    ON user_info.ID = user_image_info.user_id
                )
                ON material_info.MD5 = user_image_info.MD5
                WHERE material_info.category = %s AND user_info.tel = %s
                ORDER BY user_image_info.update_time DESC;
        '''
        if category == '0':
            selectSQL = selectSQL.replace('material_info.category = %s AND ', '')
            resultSelect = MySqlCRUD.selectImpl(selectSQL, tel)
        else:
            resultSelect = MySqlCRUD.selectImpl(selectSQL, category, tel)
        jsonStr = json.dumps(resultSelect, ensure_ascii=False)
        jsonStr = jsonStr.replace('/media', '/static')
        dataArray = json.loads(jsonStr, encoding="utf-8")
        return dataArray

